#include<bits/stdc++.h>
#define int long long
using namespace std;
map<pair<int,int>,int>mp;
struct f{int x,y;}a[500005];
int dis[500005],ans,vis[500005],n;
queue<int>q;
vector<int>s[500005];
void bfs(int SS){
	for(int i=1;i<=n;i++){dis[i]=1e18;}
	q.push(SS);dis[SS]=0;
	while(q.size()){
		int p=q.front();q.pop();
		for(int i=0;i<s[p].size();i++){
			int y=s[p][i];if(dis[y]==1e18){q.push(y);dis[y]=dis[p]+1;}
		}
	}
	for(int i=1;i<=n;i++) ans+=dis[i];
}
int siz[500005];
void dfs(int p,int fa){
	siz[p]=1;
	for(int i=0;i<s[p].size();i++){
		int y=s[p][i];if(y==fa) continue;
		dfs(y,p);siz[p]+=siz[y];
		ans+=(siz[y]*(n-siz[y]));
	}
}
int ls[500005],rs[500005],siz1[500005];
signed main(){
	freopen("game.in","r",stdin);
	freopen("game.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>n;int l=1e18,r=0;
	for(int i=1;i<=500000;i++) ls[i]=1e18;
	for(int i=1;i<=n;i++){
		cin>>a[i].x>>a[i].y;siz[i]=1;
		mp[make_pair(a[i].x,a[i].y)]=i;
		l=min(l,a[i].x);r=max(r,a[i].x);
		ls[a[i].x]=min(ls[a[i].x],a[i].y);rs[a[i].x]=max(rs[a[i].x],a[i].y);
	}
	int m=0;
	for(int i=1;i<=n;i++){
		int v=mp[make_pair(a[i].x,a[i].y)];
		if(mp[make_pair(a[i].x+1,a[i].y)]){
			int u=mp[make_pair(a[i].x+1,a[i].y)];m++;
			s[u].push_back(v);
			s[v].push_back(u);
		}
		if(mp[make_pair(a[i].x,a[i].y+1)]){
			int u=mp[make_pair(a[i].x,a[i].y+1)];m++;
			s[u].push_back(v);
			s[v].push_back(u);
		}
	}
	if(n<=4000){
		for(int i=1;i<=n;i++){bfs(i);}
		cout<<ans/2;return 0;
	}
	if(m==n-1){
		dfs(1,0);
		cout<<ans;return 0;
	}
	int anss=0;
	for(int i=l;i<=r;i++){
		int num=0;
		for(int j=ls[i];j<=rs[i];j++){
			int u=mp[make_pair(i,j)];
			siz1[u]=siz[u];num+=siz[u];
		}anss=num;
		for(int j=ls[i];j<=rs[i];j++){
			int u=mp[make_pair(i,j)];
			if(mp[make_pair(i+1,j)]){
				siz[mp[make_pair(i+1,j)]]+=siz[u];
				ans+=siz[u]*(n-anss);
			}
			else{
				siz[mp[make_pair(i,j+1)]]+=siz[u];
				ans+=siz[u]*(n-anss);
			}
		}
		int num1=0;
		for(int j=ls[i];j<=rs[i];j++){
			int u=mp[make_pair(i,j)];
			num1+=siz1[u];
			ans=(ans+(num-num1)*num1);
		}
	}
	cout<<ans;
	return 0;
}
/*
6
1 1
1 2
1 3
2 1
2 2
2 3
3 3
4 3
*/
